//
//  YFMomentResponseModel.m
//  CoollangTennisBall
//
//  Created by Coollang on 2017/9/26.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFMomentResponseModel.h"
#import "YFMomentModel.h"
#import "NSString+Extension.h"

@implementation YFMomentResponseModel
+ (NSDictionary *)defaultPropertyValues {
    return @{
             @"momentModel":[NSNull null],
             @"postID":@"",
             @"responseID":@"",
             @"creatTime":@"",
             @"text":@"",
             @"parentID":@"",
             @"rUserID":@"",
             @"rUserName":@"",
             @"quote":[NSNull null],
             };
}

+ (NSArray<NSString *> *)ignoredProperties {
    return @[@"commentHeight",@"commentString"];
}

- (void)parseMomentResponseModelWithDict:(NSDictionary *)dict withmomentModel:(YFMomentModel *)momentModel {
    self.postID = [NSString checkIfNullWithString:dict[@"PostID"]];
    self.responseID = [NSString checkIfNullWithString:dict[@"RID"]];
    self.creatTime = [NSString checkIfNullWithString:dict[@"CreateTime"]];
    self.text = [NSString checkIfNullWithString:dict[@"Text"]];
    self.parentID = [NSString checkIfNullWithString:dict[@"ParentID"]];
    self.rUserID = [NSString checkIfNullWithString:dict[@"RUserID"]];
    self.rUserName = [NSString checkIfNullWithString:dict[@"RUserName"]];
    self.quote = [YFMomentsPersonModel modelWithJSON:dict[@"Quote"]];
    self.momentModel = momentModel;
    [self CreatCommentAttrString];
}

/**
 计算评论需要的高度,生成评论文字
 */
- (void)CreatCommentAttrString {
    NSString *userNameLink;
    NSString *commentText;
    NSMutableAttributedString *text;
    if (self.quote == nil) {
        ///是一条评论
        userNameLink = [NSString stringWithFormat:@"%@: ",self.rUserName];
        commentText = self.text;
        text  = [[NSMutableAttributedString alloc] initWithString:[userNameLink stringByAppendingString:commentText]];
    }else{
        ///是一条回复
        userNameLink = [NSString stringWithFormat:@"%@回复%@: ",self.rUserName,self.quote.userName];
        commentText = self.text;
        text  = [[NSMutableAttributedString alloc] initWithString:[userNameLink stringByAppendingString:commentText]];
    }
    text.lineSpacing = 2;
    text.font = [UIFont systemFontOfSize:kTextFont];
    text.color = [UIColor darkTextColor];
    [text addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:kTextFont] range:NSMakeRange(0, self.rUserName.length)];
    [text addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:kTextFont] range:NSMakeRange(self.rUserName.length+2, self.quote.userName.length)];
    self.commentString = text;
    
    __weak typeof(self)weakSelf = self;
    if (self.quote == nil) {
        ///是一条评论
        [self.commentString setTextHighlightRange:NSMakeRange(0, self.rUserName.length)
                                            color:[UIColor colorWithHex:0x576b95]
                                  backgroundColor:[UIColor grayColor]
                                        tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                                            [[NSNotificationCenter defaultCenter] postNotificationName:kNoticeWatchUserInfo object:nil userInfo:@{@"userID":weakSelf.rUserID}];
                                        }];
    }else{
        ///是一条回复
        [self.commentString setTextHighlightRange:NSMakeRange(0, self.rUserName.length)
                                            color:[UIColor colorWithHex:0x576b95]
                                  backgroundColor:[UIColor grayColor]
                                        tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                                            [[NSNotificationCenter defaultCenter] postNotificationName:kNoticeWatchUserInfo object:nil userInfo:@{@"userID":weakSelf.rUserID}];
                                        }];
        [self.commentString setTextHighlightRange:NSMakeRange(self.rUserName.length+2, self.quote.userName.length)
                                            color:[UIColor colorWithHex:0x576b95]
                                  backgroundColor:[UIColor grayColor]
                                        tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                                            [[NSNotificationCenter defaultCenter] postNotificationName:kNoticeWatchUserInfo object:nil userInfo:@{@"userID":weakSelf.quote.userID}];
                                        }];
    }
    
    [self caucalCommentHeight];
}
- (void)caucalCommentHeight {
    //计算出高度
    YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(0, 0, kTextLabelWidth - 10, CGFLOAT_MAX)];
    label.attributedText = self.commentString;
    label.numberOfLines = 0;
    [label sizeToFit];
    self.commentHeight = label.frame.size.height + 4;
}

/**
 创建评论模型
 @param         text                评论的内容
 @param         responseID          服务器返回的评论ID
 @return                            数据模型
 */
+ (YFMomentResponseModel *)creatNewCommentWithText:(NSString *)text
                                            postID:(NSString *)postID
                                        responseID:(NSString *)responseID {
    YFMomentResponseModel *model = [[YFMomentResponseModel alloc] init];
    model.text = text;
    model.postID = postID;
    model.responseID = responseID;
    model.rUserID = [User getLastUser].userInforItem.userID;
    model.rUserName = [User getLastUser].userInforItem.name;
    [model CreatCommentAttrString];
    return model;
}

/**
 创建回复模型
 @param         text                回复的内容
 @param         responseID          服务器返回的评论ID
 @param         commentModel        回复的那条评论的数据模型
 @return                            数据模型
 */
+ (YFMomentResponseModel *)creatNewResponseWithText:(NSString *)text
                                         responseID:(NSString *)responseID
                                       commentModel:(YFMomentResponseModel *)commentModel {
    YFMomentResponseModel *model = [YFMomentResponseModel new];
    model.momentModel = commentModel.momentModel;
    model.text = text;
    model.postID = commentModel.postID;
    model.responseID = responseID;
    model.parentID = commentModel.responseID;
    model.rUserID = [User getLastUser].userInforItem.userID;
    model.rUserName = [User getLastUser].userInforItem.name;
    YFMomentsPersonModel *quotoModel = [YFMomentsPersonModel momentPersonModelWithUID:commentModel.rUserID userName:commentModel.rUserName];
    quotoModel.userID = commentModel.rUserID;
    quotoModel.userName = commentModel.rUserName;
    model.quote = quotoModel;
    [model CreatCommentAttrString];
    return model;
}

@end
/**-----------------------------------------------------------**/
#pragma mark - 图片对象
//@interface YFMomentImageModel ()
///** 图片大小 */
//@property (nonatomic, assign) CGSize imageOriginSzie;
//@property (nonatomic, assign) BOOL isLongImage;
//@end

@implementation YFMomentImageModel

+ (NSDictionary *)defaultPropertyValues {
    return  @{
              @"imageUrl":@"",
              @"imageOriginWidth":@0,
              @"imageOriginheight":@0,
              @"isLongImage":@0,
              };
}


- (void)setImageUrl:(NSString *)imageUrl {
    _imageUrl = imageUrl;
    if ([imageUrl containsString:@"*"] && [imageUrl containsString:@"-"]) {
        NSString *sizeString = [imageUrl componentsSeparatedByString:@"-"].lastObject;
        _imageOriginWidth = [sizeString componentsSeparatedByString:@"*"].firstObject.floatValue;
        _imageOriginHeight = [sizeString componentsSeparatedByString:@"*"].lastObject.floatValue;
    }else {
        _imageOriginHeight = 0;
        _imageOriginWidth = 0;
    }
    if (_imageOriginWidth > 0 &&
        (_imageOriginWidth > 2.0 * _imageOriginHeight || _imageOriginHeight > 2.0 * _imageOriginWidth)) {
        _isLongImage = YES;
    }else {
        _isLongImage = NO;
    }
}

+ (instancetype)momentImageModelWithUrl:(NSString *)imgUrl {
    YFMomentImageModel *imageModel = [[YFMomentImageModel alloc] init];
    imageModel.imageUrl = imgUrl;
    return imageModel;
}

@end
/**-----------------------------------------------------------**/
#pragma mark - 点赞/回复人的对象
@implementation YFMomentsPersonModel
+ (instancetype)momentPersonModelWithUID:(NSString *)uid userName:(NSString *)username {
    YFMomentsPersonModel *personModel = [[YFMomentsPersonModel alloc] init];
    personModel.userID = uid;
    personModel.userName = username;
    return personModel;
}

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"userID": @"UserID",
             @"userName": @"UserName",
             };
}

@end

